package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCompany;
import org.linlinjava.litemall.db.domain.LitemallCompanyMember;
import org.linlinjava.litemall.db.service.LitemallCompanyMemberService;
import org.linlinjava.litemall.db.service.LitemallCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/companyMember")
@Validated
public class AdminCompanyMemberController {
    private final Log logger = LogFactory.getLog(AdminCompanyMemberController.class);

    @Autowired
    private LitemallCompanyMemberService companyMemberService;

    @RequiresPermissions("admin:companyMember:list")
    @RequiresPermissionsDesc(menu = {"企业管理", "企业员工管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String companyName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallCompanyMember> companyList = companyMemberService.querySelective(companyName, page, limit, sort, order);
        return ResponseUtil.okList(companyList);
    }

    private Object validate(LitemallCompanyMember member) {
        String telephone = member.getTelephone();
        if (StringUtils.isEmpty(telephone)) {
            return ResponseUtil.fail(401, "电话必填");
        }
        int companyId = member.getCompanyId();
        if (member.getCompanyId() != null && companyId != 0) {
            return ResponseUtil.fail(401, "所属企业必选");
        }
        return null;
    }

    @RequiresPermissions("admin:companyMember:create")
    @RequiresPermissionsDesc(menu = {"企业管理", "企业员工管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallCompanyMember member) {
        Object error = validate(member);
        if (error != null) {
            return error;
        }
        companyMemberService.add(member);
        return ResponseUtil.ok(member);
    }

    @RequiresPermissions("admin:companyMember:read")
    @RequiresPermissionsDesc(menu = {"企业管理", "企业员工管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        LitemallCompanyMember member = companyMemberService.findById(id);
        return ResponseUtil.ok(member);
    }

    @RequiresPermissions("admin:companyMember:update")
    @RequiresPermissionsDesc(menu = {"企业管理", "企业员工管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallCompanyMember member) {
        Object error = validate(member);
        if (error != null) {
            return error;
        }
        if (companyMemberService.updateById(member) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(member);
    }

    @RequiresPermissions("admin:companyMember:delete")
    @RequiresPermissionsDesc(menu = {"企业管理", "企业员工管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallCompanyMember member) {
        Integer id = member.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        companyMemberService.deleteById(id);
        return ResponseUtil.ok();
    }

}
